package top.csy8888.deadqueue.service;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.csy8888.deadqueue.consts.RabbitConstants;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author CSY
 * @date 2025 - 10 - 16
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ProducerService {
    private final RabbitTemplate rabbitTemplate;

    private final RedisTemplate<String,Integer> redisTemplate;

    /**
     * 预热重试次数
     */
    @PostConstruct
    public void initRetry(){
        redisTemplate.opsForValue().set("retry",0,30, TimeUnit.MINUTES);
    }

    public void send(String message) {
        CorrelationData cdata = new CorrelationData(UUID.randomUUID().toString());

        rabbitTemplate.convertAndSend(RabbitConstants.EXCHANGE_NAME, RabbitConstants.ROUTING_KEY, message, cdata);
    }
}
